import 'dart:convert';

import 'package:client_flutter_android/bean/cabinet.dart';
import 'package:client_flutter_android/widgets/btn.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class Box extends StatelessWidget {
  final int index;
  final Cabinet? cabinet;
  final WebViewController? controller;

  const Box({super.key, required this.index, this.cabinet, this.controller});

  @override
  Widget build(BuildContext context) {
    if (index == 23 || index == 24) {
      return const SizedBox(
        width: 120,
        height: 130,
      );
    }

    String tag = index > 23 ? (index - 2).toString() : index.toString();
    String czh=cabinet?.czh ?? "";
    if(czh!=""){
      czh=cabinet!.is400AF?"S$czh":czh;
    }

    return Btn(
      tag: tag,
      title: czh,
      color: cabinet?.occupied ?? false ? Colors.green : Colors.red,
      onTap: () async {
        try {
          //开门
          if(cabinet!=null&&controller!=null){
              controller?.runJavaScript("openDoor(${jsonEncode(cabinet)})");
          }
          
        } catch (e) {
          debugPrint(e.toString());
        }
      },
      onTagTap: () async {},
    );
  }
}
